package com.neuq.animaladoption.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neuq.animaladoption.mapper.AnimalMapper;
import com.neuq.animaladoption.pojo.Animal;
import com.neuq.animaladoption.pojo.PageBean;
import com.neuq.animaladoption.service.AnimalService;
import com.neuq.animaladoption.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class AnimalServiceImpl implements AnimalService {

    @Autowired
    private AnimalMapper animalMapper;

    @Override
    public void add(Animal animal) {
        animal.setCreateTime(LocalDateTime.now());
        animal.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");
        animal.setCreateUser(userid);
        animalMapper.add(animal);
    }

    @Override
    public PageBean<Animal> list(Integer pageNum, Integer pageSize, String animalType, String animalState,Integer shelterId) {
        PageBean<Animal> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Animal> as = animalMapper.list(animalType,animalState,shelterId);
        Page<Animal> p = (Page<Animal>) as;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public PageBean<Animal> listLimit(Integer pageNum, Integer pageSize, Integer shelterId, String animalType) {
        PageBean<Animal> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Animal> as = animalMapper.listLimit(shelterId,animalType);
        Page<Animal> p = (Page<Animal>) as;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Animal findById(Integer id) {
        return animalMapper.findById(id);
    }

    @Override
    public void update(Animal animal) {
        animal.setUpdateTime(LocalDateTime.now());
        animalMapper.update(animal);
    }

    @Override
    public void deleteById(Integer id) {
        animalMapper.deleteById(id);
    }


}
